/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utilities;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class EmailReminder {
    public static void sendreminder(final String src, final String password,
            String dst, String title, String content) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(src, password);
                }
            });
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(src));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(dst));
            message.setSubject(title);
            message.setText(content);
        } catch (MessagingException ex) {
            Logger.getLogger(EmailReminder.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Transport.send(message);
            Prompt.warning("Email sent successfully");
        } catch (MessagingException ex) {
            Logger.getLogger(EmailReminder.class.getName()).log(Level.SEVERE, null, ex);
            Prompt.warning("Authentication Failure. Please close and retry.");
        }
        
    }
}